from django.db import models



class LoginLogs(models.Model):
    user = models.CharField(max_length=32, verbose_name='登录用户', null=True)
    ip = models.GenericIPAddressField(verbose_name='用户地址', null=True)
    ctime = models.DateTimeField(auto_now_add=True, verbose_name='时间')

    class Meta:
        db_table = "LoginLogs"
        verbose_name = "平台登录"
        verbose_name_plural = '平台登录'

    def __str__(self):
        return self.user

# Create your models here.

class AssetLoginUser(models.Model):
    username = models.CharField(max_length=64, verbose_name="用户名", default='root', null=True, blank=True,unique=True)
    password = models.CharField(max_length=256, blank=True, null=True, verbose_name='密码')
    remarks = models.CharField(max_length=10240, verbose_name="备注", null=True, blank=True)
    status=models.BooleanField(default=0,verbose_name="禁用")
    uptime = models.DateTimeField(auto_now_add=True, verbose_name="更新时间")

    addtime = models.DateTimeField(null=True, verbose_name='创建时间', blank=True,auto_now=True)

    def __str__(self):
        return self.username

    class Meta:
        verbose_name = '登录用户'
        verbose_name_plural = '登录用户'

class assetgroup(models.Model):
    groupname=models.CharField(max_length=100,verbose_name="资产组名称",unique=True)
    status=models.IntegerField(default=1,verbose_name="资产组状态")  # 1表示失效
    addtime = models.DateTimeField(auto_now_add=True, verbose_name="添加时间")
    uptime = models.DateTimeField(auto_now_add=True, verbose_name="更新时间")
    remarks = models.CharField(max_length=2000, verbose_name="描述")


class ScriptsGroup(models.Model):
    groupname=models.CharField(max_length=100,verbose_name="脚本组名称",unique=True)
    status=models.IntegerField(default=1,verbose_name="脚本组状态")  # 1表示失效
    addtime = models.DateTimeField(auto_now_add=True, verbose_name="添加时间")
    path=models.CharField(max_length=1000,verbose_name="脚本路径",default="/root/.devops/")
    uptime = models.DateTimeField(auto_now_add=True, verbose_name="更新时间")
    remarks = models.CharField(max_length=2000, verbose_name="描述")


    def __str__(self):
        return self.groupname

    class Meta:
        verbose_name = '主机类型'
        verbose_name_plural = '主机类型'


class Scripts(models.Model):
    zwname = models.CharField(max_length=200, verbose_name="脚本名称[中文]", unique=True)
    qpname = models.CharField(max_length=200,verbose_name="脚本名称[全拼]",unique=True)
    context= models.TextField(max_length=200000,verbose_name="脚本内容")
    options = models.TextField(max_length=2000, verbose_name="使用声明")
    addtime = models.DateTimeField(auto_now_add=True, verbose_name="添加时间")
    uptime = models.DateTimeField(auto_now_add=True, verbose_name="更新时间")
    remarks = models.CharField(max_length=2000, verbose_name="描述")
    scriptsgroup = models.ForeignKey('ScriptsGroup', on_delete=False, default=1, verbose_name="脚本组")

    def __str__(self):
        return self.zwname

    class Meta:
        verbose_name = '脚本'
        verbose_name_plural = '脚本'


class HostType(models.Model):
    name = models.CharField(max_length=200, verbose_name='名称')
    type = models.CharField(max_length=200, verbose_name='类型', unique=True)
    description = models.CharField(max_length=2000, verbose_name="描述")
    addtime = models.DateTimeField(auto_now_add=True, verbose_name="添加时间")

    def __str__(self):
        return self.type

    class Meta:
        verbose_name = '主机类型'
        verbose_name_plural = '主机类型'

class initenv(models.Model):
    network_ip=models.GenericIPAddressField(verbose_name="主机 IP")
    username=models.CharField(max_length=200,verbose_name="登录用户")
    addtime = models.DateTimeField(auto_now_add=True, verbose_name="添加时间")
    password = models.CharField(max_length=200,verbose_name='登录密码')
    port = models.IntegerField(verbose_name="登录端口", default='65534', null=True, blank=True)
    scripts=models.ForeignKey('Scripts',verbose_name="推送脚本",on_delete=False)
    remarks = models.CharField(max_length=2000, verbose_name="描述")
    status=models.CharField(max_length=200,verbose_name="推送状态",default="已经添加任务")
    flag=models.IntegerField(verbose_name="添加任务状态",default=0) # 0表示资产添加，1表示远端添加

class AssetInfo(models.Model):
    hostname = models.CharField(max_length=64, verbose_name='主机名', unique=True)
    network_ip = models.GenericIPAddressField(verbose_name='内网IP', unique=True)
    inner_ip = models.GenericIPAddressField(verbose_name='外网IP', null=True)

    # 可以自动获取
    system = models.CharField(max_length=128, verbose_name='系统版本', null=True, blank=True)
    cpu = models.CharField(max_length=64, verbose_name='CPU', null=True, blank=True)
    memory = models.CharField(max_length=64, verbose_name='内存', null=True, blank=True)
    disk = models.CharField(max_length=256, verbose_name="硬盘", null=True, blank=True)

    bandwidth = models.IntegerField(verbose_name='外网带宽', null=True, blank=True, default="4")
    port = models.IntegerField(verbose_name="登录端口", default='65534', null=True, blank=True)
    is_active = models.BooleanField(default=True, verbose_name='激活')
    addtime = models.DateTimeField(auto_now_add=True, verbose_name="添加时间")
    uptime = models.DateTimeField(auto_now_add=True, verbose_name="更新时间")
    '''host type m2m'''
    # type = models.ForeignKey(HostType, verbose_name="主机类型",on_delete=False)
    '''user m2m'''
    user = models.ForeignKey('AssetLoginUser', on_delete=False, verbose_name="登录用户")
    '''servicegroup m2m'''
    # servicegroup = models.ManyToManyField(ServiceGroup,verbose_name="服务组")
    # fongroup = models.ManyToManyField(FunctionGroup,verbose_name="功能组")
    assetgroup = models.ForeignKey('assetgroup', on_delete=False, default=1, verbose_name="资产组")
    remarks = models.CharField(max_length=2000, verbose_name="描述")

    def __str__(self):
        return self.hostname

    class Meta:
        verbose_name = '主机信息'
        verbose_name_plural = '主机信息'

class CityInfos(models.Model):
    assetgroupid=models.ForeignKey(assetgroup,on_delete=False,verbose_name="城市id")
    management = models.ForeignKey(AssetInfo,on_delete=False,verbose_name="主控机")
    addtime = models.DateTimeField(auto_now_add=True, verbose_name="添加时间")
    # fongroup = models.ManyToManyField(FunctionGroup, verbose_name="功能组")
    # mongroup = models.ManyToManyField(MonitorGroup, verbose_name="信息展示组")
    # mulgroup = models.ManyToManyField(MultifunctionGroudp, verbose_name="多功能操作组")
    serverlastuptime = models.DateTimeField(verbose_name="服务端最后一次更新时间")
    clientlastuptime = models.DateTimeField(verbose_name="客户端最后一次更新时间")
    serversvnversion=models.IntegerField(verbose_name="服务端svn版本号",default=0)
    clientvnversion = models.IntegerField(verbose_name="客户端svn版本号", default=0)


    class Meta:
        ordering = ['-addtime']
        verbose_name = '城市'
        verbose_name_plural = '城市'





'''
服务组
'''

class ServiceGroup(models.Model):
    zwname = models.CharField(max_length=200, verbose_name="服务组名称[中文]")
    status=models.IntegerField(default=1,verbose_name="组状态 1表示失效")  # 1表示失效
    addtime = models.DateTimeField(auto_now_add=True, verbose_name="添加时间")
    uptime = models.DateTimeField(auto_now_add=True, verbose_name="更新时间")
    remarks = models.CharField(max_length=2000, verbose_name="描述")

    def __str__(self):
        return self.zwname

    class Meta:
        verbose_name = '服务组'
        verbose_name_plural = '服务组'


'''
服务
'''


class Services(models.Model):
    zwname = models.CharField(max_length=200, verbose_name="服务名称[中文]")
    qpname = models.CharField(max_length=200, verbose_name="服务名称[全拼]", unique=True)
    shellscripts = models.ForeignKey(Scripts, on_delete=False, verbose_name="脚本名称")
    start_cmd = models.CharField(max_length=2000, verbose_name="服务启动参数")
    stop_cmd = models.CharField(max_length=2000, verbose_name="服务停止参数")
    status_cmd = models.CharField(max_length=2000, verbose_name="服务状态参数")
    group = models.ManyToManyField(ServiceGroup, verbose_name="所属组")
    status=models.IntegerField(default=1,verbose_name="脚本状态")  # 1表示失效
    addtime = models.DateTimeField(auto_now_add=True, verbose_name="添加时间")
    uptime = models.DateTimeField(auto_now_add=True, verbose_name="更新时间")
    remarks = models.CharField(max_length=2000, verbose_name="描述")

    def __str__(self):
        return self.zwname

    class Meta:
        verbose_name = '服务'
        verbose_name_plural = '服务'


'''
功能组
'''


class FunctionGroup(models.Model):
    zwname = models.CharField(max_length=200, verbose_name="功能组名称[中文]")
    # name = models.CharField(max_length=200, verbose_name="功能组名称[全拼]", unique=True)
    workpath = models.CharField(max_length=200, verbose_name='标志')
    autolinenum = models.IntegerField(verbose_name="首页自动换行数据")
    status=models.IntegerField(default=1,verbose_name="状态")  # 1表示失效
    addtime = models.DateTimeField(auto_now_add=True, verbose_name="添加时间")
    uptime = models.DateTimeField(auto_now_add=True, verbose_name="更新时间")
    remarks = models.CharField(max_length=2000, verbose_name="描述")

    def __str__(self):
        return self.zwname

    class Meta:
        verbose_name = '功能组'
        verbose_name_plural = '功能组'


'''
功能
'''


class Functions(models.Model):
    zwname = models.CharField(max_length=100, verbose_name='功能名称[中文]')
    # qpname = models.CharField(max_length=100, verbose_name='功能名称[全拼]')
    groupname = models.ForeignKey(FunctionGroup,on_delete=False, verbose_name='所属功能组')
    shellscripts = models.ForeignKey(Scripts, on_delete=False, verbose_name="脚本名称")
    dependonservice = models.ForeignKey(Services, on_delete=False, verbose_name="服务依赖")
    # dependonhosttype = models.ForeignKey(HostType, on_delete=False, verbose_name="主机类型依赖")
    argvs = models.CharField(max_length=2000, verbose_name='脚本参数')
    isactived=models.BooleanField(verbose_name="激活",default=True)
    # status=models.IntegerField(default=1,verbose_name="状态")  # 1表示失效
    addtime = models.DateTimeField(auto_now_add=True, verbose_name="添加时间")
    uptime = models.DateTimeField(auto_now_add=True, verbose_name="更新时间")
    remarks = models.CharField(max_length=2000, verbose_name="描述")

    def __str__(self):
        return self.zwname

    class Meta:
        verbose_name = '功能'
        verbose_name_plural = '功能'



'''
cobbler add system 

{"name":system_info.get("name"),"interface":interface,
                                 "gateway":system_info.get("gateway"),"hostname":hostname,"mac_address":mac_address,
                                 "netmask":netmask,"ip_address":ip_address,
                                 "kernel_options":json.dumps(system_info.get("kernel_options")),
                                 "name_servers":system_info.get("name_servers"),
                                 "netboot_enabled":system_info.get("netboot_enabled"),
                                 "kickstart":system_info.get("kickstart"),"profile":system_info.get("profile")
                                 }
'''


class CobblerSystemInit(models.Model):
    hostname = models.CharField(max_length=1000, verbose_name='主机名称')
    interface = models.CharField(max_length=1000, verbose_name='网卡接口')
    gateway = models.CharField(max_length=1000, verbose_name='网关')
    mac_address = models.CharField(max_length=1000, verbose_name='MAC')
    netmask = models.CharField(max_length=1000, verbose_name='子网掩码')
    ip_address = models.CharField(max_length=1000, verbose_name='ip地址')
    kernel_options = models.CharField(max_length=1000, verbose_name='内核参数')
    name_servers = models.CharField(max_length=1000, verbose_name='dns地址')
    kickstart = models.CharField(max_length=1000, verbose_name='ks启动文件')
    netboot_enabled =models.BooleanField(verbose_name="激活",default=True)
    profile = models.CharField(max_length=1000, verbose_name='系统配置文件')

    starttime = models.CharField(max_length=100,verbose_name="开始安装时间",default="")
    endtime = models.CharField(max_length=100,verbose_name="结束安装时间",default="")
    successcount = models.IntegerField(verbose_name="成功安装系统的次数",default=0)
    installcount = models.IntegerField(verbose_name="本系统安装的总次数",default=0)
    status = models.CharField(max_length=100,verbose_name="系统安装状态",default="")

    # status=models.IntegerField(default=1,verbose_name="状态")  # 1表示失效
    addtime = models.DateTimeField(auto_now_add=True, verbose_name="添加时间")
    uptime = models.DateTimeField(auto_now_add=True, verbose_name="更新时间")
    remarks = models.CharField(max_length=2000, verbose_name="描述")

    def __str__(self):
        return self.hostname

    class Meta:
        verbose_name = '系统安装记录'
        verbose_name_plural = '系统安装记录'